<?php
namespace app\admin\controller;

class Uploads
{
    public function index()
    {
        return view('index');
    }
public function ahxupload()
{		
		$date=date('Ymd',time()).'/';
		$uploaddir = './uploads/'.$date;
		if(!is_dir($uploaddir))
		{
				mkdir($uploaddir,0777,true);
		}
		
		$newName=md5(time().mt_rand(10000,99999)).".".$this->get_fileext($_FILES['file']['name']);
		$uploadfile = $uploaddir.$newName;
		
		move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile);
		$this->createImageThumb($uploadfile);
		echo  '<script>
					
					$("#imgDetail").val($("#imgDetail").val()+"'.$date.$newName.'"+"|");
			  </script>';
}
	
	function get_fileext($file='')
	{
		return strtolower(substr(strrchr($file,'.'),1));
	}
	/**
		imageinfo($img='')  $img图片地址
		功能：获取图片信息
	*/
	function imageinfo($img='')
	{
		$info=array();
		$t=basename($img);
		$t=explode('.',$t);
		$info['name']=$t[0];
		$info['size']=filesize($img);
		$imageinfo=getimagesize($img);
		$info['width']=$imageinfo[0];
		$info['height']=$imageinfo[1];
		$info['width_height']=$imageinfo[3];
		$info['mime']=$imageinfo['mime'];
		unset($imageinfo);
		$imageinfo=pathinfo($img);
		$info['path']=$imageinfo['dirname'].'/'; 
		$info['type']=strtolower($imageinfo['extension']);
		unset($imageinfo,$name);
		return $info;
	}

/**
	createImageThumb($img='',$width='600')  $img图片地址 $width缩略图宽度，高度自动按比例缩放
	功能：生成缩略图
*/
	function createImageThumb($img='',$width='300')
	{
		$_image_info=$this->imageinfo($img);
		$_image_ext=explode("/",$_image_info['mime']);
		$_image_ext=$_image_ext[1]=='jpeg'?'jpg':$_image_ext[1];

		$im=imagecreatetruecolor($width,$_image_info['height']*($width/$_image_info['width']));
		$_createfun='imagecreatefrom'.($_image_ext=='jpg'?'jpeg':$_image_ext);
		$_imagefun='image'.($_image_ext=='jpg'?'jpeg':$_image_ext);

		$thumbim = $_createfun($img);

		if(function_exists('imagecopyresampled'))
		{
			imagecopyresampled($im, $thumbim, 0, 0, 0, 0, $width, $_image_info['height']*($width/$_image_info['width']), $_image_info['width'], $_image_info['height']);
		}
		else
		{
			imagecopyresized($im, $thumbim, 0, 0, 0, 0, $width, $_image_info['height']*($width/$_image_info['width']), $_image_info['width'], $_image_info['height']);
		}
			
		if($_image_ext=='jpg')
		{
			$_imagefun($im,substr($img,0,'-'.(strlen($_image_ext)+1)).'x'.$width.'.'.$this->get_fileext($img),100);
		}
		else
		{
			$_imagefun($im,substr($img,0,'-'.(strlen($_image_ext)+1)).'x'.$width.'.'.$this->get_fileext($img));
		}
	}

	function get_thumb_path($img='',$width=600)
	{
		$_thumb_path=dirname($img).'/'.basename($img,'.'.$this->get_fileext($img)).'x'.$width.'.'.$this->get_fileext($img);

		return is_file($_thumb_path)?$_thumb_path:$img;
	}
	



}